namespace PickGold.Charting.Design
{
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms.Design;

    internal class FlagsEnumUITypeEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _edSvc;
        private Type _enumType;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (((context != null) && (context.Instance != null)) && (provider != null))
            {
                this._edSvc = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
                if (this._edSvc == null)
                {
                    return value;
                }
                if (value != null)
                {
                    this._enumType = value.GetType();
                }
                else if ((context != null) && (context.PropertyDescriptor != null))
                {
                    this._enumType = context.PropertyDescriptor.PropertyType;
                }
                if (this._enumType != null)
                {
                    FlagsEnumCheckedListBox control = new FlagsEnumCheckedListBox(value, this._enumType);
                    this._edSvc.DropDownControl(control);
                    value = control.GetNewValue();
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if ((context != null) && (context.Instance != null))
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }
    }
}

